package com.example.audiostreaming.implement;

import com.example.audiostreaming.data.HistoryDao;
import com.example.audiostreaming.data.IHistoryDao;
import com.example.audiostreaming.data.IHistoryDaoCallback;
import com.example.audiostreaming.interfaces.IHistoryPresenter;
import com.example.audiostreaming.interfaces.IHistoryPresenterCallback;
import com.example.audiostreaming.utils.UtilConstants;
import com.example.audiostreaming.utils.UtilLog;
import com.ximalaya.ting.android.opensdk.model.track.Track;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.schedulers.Schedulers;

import static com.example.audiostreaming.base.BaseApplication.getHandler;

public class HistoryPresenter implements IHistoryPresenter {
    private static final String TAG = "HistoryPresenter";
    private List<IHistoryPresenterCallback> mCallbacks = new ArrayList<>();


    private final IHistoryDao mHistoryDao;
    private final HistoryDaoCallback mHistoryDaoCallback;
    private List<Track> mCurrentHistories=null;
    private Track mCurrentAddTrack=null;

    private HistoryPresenter() {
        mHistoryDao = new HistoryDao();
        mHistoryDaoCallback = new HistoryDaoCallback();
        mHistoryDao.setCallback(mHistoryDaoCallback);
        listHistories();
    }
    private static HistoryPresenter sHistoryPresenter = null;
    public static HistoryPresenter getHistoryPresenter() {
        if(sHistoryPresenter == null) {
            synchronized(HistoryPresenter.class) {
                if(sHistoryPresenter == null) {
                    sHistoryPresenter = new HistoryPresenter();
                }
            }
        }
        return sHistoryPresenter;
    }


    @Override
    public void listHistories() {
        //可观察的
        Observable.create(new ObservableOnSubscribe<Object>() {
            @Override
            public void subscribe(ObservableEmitter<Object> emitter) throws Exception {
                if (mHistoryDao != null) {
                    mHistoryDao.listHistories();
                }
            }
        }).subscribeOn(Schedulers.io()).subscribe();//在io线程里进行订阅
    }

    private boolean isDoDelAsOutOfSize = false;//Do Del是否超出尺寸
    @Override
    public void addHistory(final Track track) {
        //判断是否>=100条记录
        if (mCurrentHistories != null&&mCurrentHistories.size()>= UtilConstants.MAX_HISTORY_COUNT) {
            //不能添加，先删除最前的一条记录，再添加
            this.mCurrentAddTrack = track;
            isDoDelAsOutOfSize = true;
            delHistory(mCurrentHistories.get(mCurrentHistories.size()-1));
        }else {
            doAddHistory(track);
        }
    }

    private void doAddHistory(final Track track) {
        //可观察的
        Observable.create(new ObservableOnSubscribe<Object>() {
            @Override
            public void subscribe(ObservableEmitter<Object> emitter) throws Exception {
                if (mHistoryDao != null) {
                    mHistoryDao.addHistory(track);
                }
            }
        }).subscribeOn(Schedulers.io()).subscribe();//在io线程里进行订阅
    }

    @Override
    public void delHistory(final Track track) {
        //可观察的
        Observable.create(new ObservableOnSubscribe<Object>() {
            @Override
            public void subscribe(ObservableEmitter<Object> emitter) throws Exception {
                if (mHistoryDao != null) {
                    mHistoryDao.delHistory(track);
                }
            }
        }).subscribeOn(Schedulers.io()).subscribe();//在io线程里进行订阅
    }

    @Override
    public void cleanHistories() {
        //可观察的
        Observable.create(new ObservableOnSubscribe<Object>() {
            @Override
            public void subscribe(ObservableEmitter<Object> emitter) throws Exception {
                if (mHistoryDao != null) {
                    mHistoryDao.clearHistory();
                }
            }
        }).subscribeOn(Schedulers.io()).subscribe();//在io线程里进行订阅
    }

    @Override
    public void registerViewCallback(IHistoryPresenterCallback iHistoryPresenterCallback) {
        if(!mCallbacks.contains(iHistoryPresenterCallback)) {
            mCallbacks.add(iHistoryPresenterCallback);
        }
    }

    @Override
    public void unRegisterViewCallback(IHistoryPresenterCallback iHistoryPresenterCallback) {
        mCallbacks.remove(iHistoryPresenterCallback);
    }









    private class HistoryDaoCallback implements IHistoryDaoCallback{


        @Override
        public void onHistoryAdd(boolean isSuccess) {
            listHistories();
        }

        @Override
        public void onHistoryDel(boolean isSuccess) {
            if (isDoDelAsOutOfSize && mCurrentAddTrack!=null) {
                isDoDelAsOutOfSize = false;
                //添加当前的数据进到数据库里
                addHistory(mCurrentAddTrack);
            }
                listHistories();

        }

        @Override
        public void onHistoriesLoaded(final List<Track> tracks) {
            mCurrentHistories = tracks;
            UtilLog.d(TAG,"历史尺寸 ==> "+tracks.size());
            //通知UI更新数据
            getHandler().post(new Runnable() {
                @Override
                public void run() {
                    if (mCallbacks != null) {
                        for (IHistoryPresenterCallback callback : mCallbacks) {
                            callback.onHistoriesLoaded(tracks);
                        }
                    }
                }
            });
        }

        @Override
        public void onHistoriesClean(boolean isSuccess) {
            listHistories();
        }
    }

}
